package com.wl.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wangl
 * @desc
 * @date:2017/6/2615:55
 * @Copyright (c) 2017, DaChen All Rights Reserved.
 */
public class ThrowExceptionFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        try{
            throw new RuntimeException("throw runtime Exception");
        }catch (Exception e){
            /**
             * 需要在ctx中设置该参数
             * 否则客户端不能收到错误
             */
            ctx.set("error.error_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            ctx.set("error.exception", e);
        }
        return null;
    }
}
